package com.example.nowcodercommunity.controller;

import com.example.nowcodercommunity.dao.DiscussPostMapper;
import com.example.nowcodercommunity.entity.DiscussPost;
import com.example.nowcodercommunity.entity.User;
import com.example.nowcodercommunity.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TestMapperController {

    @Autowired
    private DiscussPostMapper mapper;

    @Autowired
    private UserService userService;

    @GetMapping("/nowcoder/discuss/{id}")
    public List<DiscussPost> getDiscussPost(@PathVariable(name = "id") int userId){
        int totalRows = mapper.selectCountDiscussPostRows(userId);
        List<DiscussPost> discussPosts = mapper.selectDiscussPostsWithinUser(userId, 0, 5);
        System.out.println(mapper.selectCountDiscussPostRows(userId));
        if(discussPosts.size()!=0){
            return discussPosts;
        }else{
            throw new RuntimeException("未查找到任何帖子信息");
        }
    }

    @GetMapping("/nowcoder/user/{id}")
    public User getUser(@PathVariable(name = "id")int id){
        User user = userService.findUserById(id);
        if(user!=null){
            return user;
        }else{
            throw new RuntimeException("未找到任何用户");
        }
    }
}
